package cn.kgc.fxy.smbms.web;

import cn.kgc.fxy.smbms.entity.User;
import cn.kgc.fxy.smbms.service.Constant;
import cn.kgc.fxy.smbms.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LogInOutController {
    @Resource
    private UserService userService;

    //to 去往,go to school
    //do 做事情

    @RequestMapping("/toLogin")
    public String toLogin(String flag){
        return "login";//WEB-INF/smbms/login.jsp
    }


    @RequestMapping("/doLogin")
    public String doLogin(String userCode, String userPassword,HttpSession session) {
        User user = userService.findLogin(userCode, userPassword);

        if (user != null) {//登录成功
            session.setAttribute(Constant.USER,user);
            return "redirect:toFrame";
        }
        return "redirect:toLogin?flag=-1";
    }
    @RequestMapping("/toFrame")
    public String toFrame(){
        return "frame";//WEB-INF/smbms/frame.jsp
    }
    // 退出登录
    @RequestMapping("/frameOut")
    public String frameOut(HttpSession session){
        session.invalidate();
        return "redirect:toLogin";
    }
}
